package com.example.course.converter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.course.bean.Classes;
import com.example.course.bean.Student;
import com.example.course.bean.Teacher;
import com.example.course.dto.ClassesDTO;
import com.example.course.service.StudentService;
import com.example.course.service.TeacherService;
import com.example.course.vo.ClassesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 班级实体与DTO/VO转换器
 *
 * @author Jasmine-drs
 * @date 2025/4/23 10:30
 */
@Component
public class ClassesConverter {

	private final StudentService studentService;
	private final TeacherService teacherService;

	public ClassesConverter(@Lazy StudentService studentService, TeacherService teacherService) {
		this.studentService = studentService;
		this.teacherService = teacherService;
	}

	/**
	 * 将ClassesDTO转换为Classes实体
	 *
	 * @param dto ClassesDTO
	 * @return Classes
	 */
	public Classes toEntity(ClassesDTO dto) {
		if (dto == null) {
			return null;
		}
		Classes entity = new Classes();
		BeanUtils.copyProperties(dto, entity);
		return entity;
	}

	/**
	 * 将Classes实体转换为ClassesVO
	 *
	 * @param entity Classes
	 * @return ClassesVO
	 */
	public ClassesVO toVO(Classes entity) {
		if (entity == null) {
			return null;
		}

		ClassesVO vo = new ClassesVO();
		BeanUtils.copyProperties(entity, vo);

		// 设置班级学生人数
		long studentCount = studentService.count(
				new LambdaQueryWrapper<Student>()
						.eq(Student::getClassesId, entity.getId())
		);
		vo.setStudentCount((int) studentCount);

		// 查找班主任信息
		Teacher teacher = teacherService.getOne(
				new LambdaQueryWrapper<Teacher>()
						.eq(Teacher::getId, entity.getTeacherId())
		);
		if (teacher != null) {
			vo.setTeacherId(teacher.getId());
			vo.setTeacherName(teacher.getName());
		}

		return vo;
	}

	/**
	 * 将Classes实体列表转换为ClassesVO列表
	 *
	 * @param entityList Classes列表
	 * @return ClassesVO列表
	 */
	public List<ClassesVO> toVOList(List<Classes> entityList) {
		if (entityList == null || entityList.isEmpty()) {
			return new ArrayList<>();
		}

		// 获取所有班级ID
		List<Integer> classesIds = entityList.stream()
				.map(Classes::getId)
				.collect(Collectors.toList());
		// 获取所有的班级内部的教师id
		List<Integer> teacherIds = entityList.stream()
				.map(Classes::getTeacherId)
				.collect(Collectors.toList());

		// 批量查询每个班级的学生人数
		Map<Integer, Integer> studentCountMap = new java.util.HashMap<>();
		for (Integer classId : classesIds) {
			long count = studentService.count(
					new LambdaQueryWrapper<Student>()
							.eq(Student::getClassesId, classId)
			);
			studentCountMap.put(classId, (int) count);
		}

		// 批量查询班主任信息
		List<Teacher> teachers = teacherService.list(
				new LambdaQueryWrapper<Teacher>()
						.in(Teacher::getId, teacherIds)
		);
		Map<Integer, Teacher> teacherMap = teachers.stream()
				.collect(Collectors.toMap(
						Teacher::getId,
						Function.identity(),
						(existing, replacement) -> existing
				));

		// 转换为VO
		return entityList.stream().map(entity -> {
			ClassesVO vo = new ClassesVO();
			BeanUtils.copyProperties(entity, vo);

			// 设置班级学生人数
			vo.setStudentCount(studentCountMap.getOrDefault(entity.getId(), 0));

			// 设置班主任信息
			Teacher teacher = teacherMap.get(entity.getTeacherId());
			if (teacher != null) {
				vo.setTeacherId(teacher.getId());
				vo.setTeacherName(teacher.getName());
			}

			return vo;
		}).collect(Collectors.toList());
	}
} 